#include "include.h"

#if USER_TKEY

//#if TKEY_CH0                  //ch0必须配置, 硬件上其他通道有借用ch0的部分控件
static tkey_ch_t tkey0 = {
    .cdpr = CDPR,
    .hw_en = 1,
    .in_ear = 0,
};
//#endif
#if TKEY_CH1
static tkey_ch_t tkey1 = {
    .cdpr = CDPR,
    .hw_en = 1,
    .in_ear = 0,
};
#endif
#if TKEY_CH2
static tkey_ch_t tkey2 = {
    .cdpr = CDPR,
    .hw_en = 1,
    .in_ear = 0,
};
#endif
#if TKEY_CH3
static tkey_ch_t tkey3 = {
    .cdpr = CDPR,
    .hw_en = 1,
    .in_ear = 0,
};
#endif
static tkey_cfg_t tkey_cfg = {
    .key[0] = NULL,                 //PB1
    .key[1] = NULL,                 //PB2
    .key[2] = NULL,                 //PE6
    .key[3] = NULL,                 //PE7
    .rels_times = RELS_TIMES,
    .fall_times = FALL_TIMES,
    .press_times = PRESS_TIMES,
    .rise_times = RISE_TIMES,
    .tkpthd = 30,                   //打印容值后, 查看按下稳定后的最小跳变大小, 该值设置成比跳变值稍小
    .tkrthd = 10,
    .tkepthd = 60,
    .tkerthd = 60,
    .llong_press_times = 400,
    .long_press_times = 60,
    .short_press_times = 15,
    .frq_long_press = 1,
    .cail_tkcnt = 40,               //延时校准,刚开机时采的CNT值不准,延时40次扫描后开始校准
    .cail_dwthd_left = 400,         //防止某些情况下容值下跌过大造成的异常校准
    .cail_dwthd_right = 5,
    .cail_upthd_left = 5,
    .cail_upthd_right = 29,         //建议设成tkpress_thd-1
    .cail_thd = 100,
};

void tkey_init(void *tkey_cfg);

void tkey_cail_set(u16 cnt,u16 dwthd_left,u16 dwthd_right,u16 upthd_left,u16 upthd_right,u16 thd){ //修改校准灵敏度
    tkey_cfg.cail_tkcnt       = cnt;
    tkey_cfg.cail_dwthd_left  = dwthd_left;
    tkey_cfg.cail_dwthd_right = dwthd_right;
    tkey_cfg.cail_upthd_left  = upthd_left;
    tkey_cfg.cail_upthd_right = upthd_right;
    tkey_cfg.cail_thd         = thd;
}

AT(.com_text.tkey.proc)
u8 get_tkey_min(void){
    return TKEY_MIN_CH;
}

AT(.com_text.tkey.proc)
u8 is_tkey_mul_en(void)
{
    return USER_TKEY;
}

AT(.com_text.tkey.proc)
u8 get_tkey_channels(u8 ch) {   //传入当前通道,获取下一个通道号
    //printf("ch:%d, TKEY_CHANNELS: 0x%x, TKEY_MAX_CH:%d, KEY_MIN_CH:%d\n",ch, TKEY_CHANNELS, TKEY_MAX_CH, TKEY_MIN_CH);
    if(ch == TKEY_MAX_CH){
        ch = TKEY_MIN_CH;
        if((TKEY_CHANNELS >> ch) & 0x1){
            return ch;
        }
    }else if((TKEY_CHANNELS >> (ch+1)) & 0x1){
        return ch+1;
    }else if((TKEY_CHANNELS >> (ch+2)) & 0x1){
        return ch+2;
    }else if((TKEY_CHANNELS >> (ch+3)) & 0x1){
        return ch+3;
    }else {
        return -1;              //不合法通道号返回-1
    }
    return 0;
}

AT(.com_text.tkey.proc)
u8 get_tkey_flag(void)          //延时校准函数，建议每次切换模式给cail_tkcnt赋一次值去校准
{
    if(tkey_cfg.cail_tkcnt == 0){
            return 0;
    }else{
        tkey_cfg.cail_tkcnt--;
        if(tkey_cfg.cail_tkcnt < 5)
                return 1;
        else
            return 0;
    }
}

AT(.com_text.tkey.delay)
u16 get_delay_time(void)        //校准值采样间隔[0-1000]us
{
    return 100;
}
//extern u32 tkbcnt[4];
//extern tkey_cfg_t *pscan_tkcb;
AT(.com_text.bsp.tkey)
void bsp_tkey_scan(void)
{   //printf("%d %d %d %d\n",(int)pscan_tkcb->tkcnt_val[1],(int)pscan_tkcb->ttimes[1],(int)pscan_tkcb->ttimes_cali[1],(int)tkbcnt[1]); //打印通道1
    u16 key = NO_KEY;
    u8 value;
    for(u8 i = 0;i < 4;i++){                            //扫描4路触摸通道
        value = tkey_get_key(i);
        if (value == KSU_SHORT_PRESS) {                     //短按抬起
            key = KU_PLAY;
            printf(" touch key[%d] short press up  \n",i);
        } else if (value == KLU_LONG_PRESS) {               //长按抬起
            key = KLU_PLAY;
            printf(" touch key[%d] long press up  \n",i);
        } else if (value == KLH_LONG_PRESS) {               //持续长按
    //        key = KH_PLAY;
            printf(" touch key[%d] long press hold  \n",i);
        }
        if (key != NO_KEY) {
            msg_enqueue(key);
            key = NO_KEY;
        }
    }
}

void bsp_tkey_init(void)
{
    tkey_cfg.key[0] = &tkey0;       //ch0必须配置, 硬件上其他通道有借用ch0的部分控件
#if TKEY_CH1
    tkey_cfg.key[1] = &tkey1;
#endif
#if TKEY_CH2
    tkey_cfg.key[2] = &tkey2;
#endif
#if TKEY_CH3
    tkey_cfg.key[3] = &tkey3;
#endif
    tkey_init(&tkey_cfg);
}
#endif  //#if USER_TKEY

#if USER_TKEY_DEBUG_EN
//通过蓝牙SPP打印输出, 避免UART飞线干扰
extern u32 tkbcnt[4];
extern tkey_cfg_t *pscan_tkcb;
AT(.text.tkey.init)
void spp_tkcnt_tx(void)
{
    char tk_buf[30];
    memset(tk_buf,0,sizeof(tk_buf));
    //打印触摸通道0数据
    snprintf(tk_buf,sizeof(tk_buf)-1,"[TK]:%d %d %d %d\n",(int)pscan_tkcb->tkcnt_val[0],(int)pscan_tkcb->ttimes[0],(int)pscan_tkcb->ttimes_cali[0],(int)tkbcnt[0]);
    bt_spp_tx((uint8_t *)tk_buf,30);
}
#endif // USER_TKEY_DEBUG_EN
